<?php defined('SMIT_PATH') or die('Access Denied');
/**
 *	forum/app.php - SwixsBoard Forum Application
 *	This is Smit Matterhorn - Build 24109
 *	
 *
 * @author		The SmitBoard Team & Community
 * @license		GNU GPL v3
 * @link		http://smitboard.sf.net
 * @since		Matterhorn
 */
 
class Core extends SmitApp
{
	function main()
	{
		//$action = $this->base->URI(2, 'show');

		/*switch($action)
		{
			default:
			break;
			case 'login':
			// haha
			break;
			case 'register':

			break;
		}*/
	}

	public function login()
	{
		if(!$this->base->session->logged_in())
		{
			$this->base->loadHandler('user');
			$rules = array(
				'username' => array(
					'title' => $this->base->lang('core.login.username'),
					'required' => 1,
					'minlength' => 4,
					'maxlength' => 50,
					'messages' => array(
						'required' => $this->base->lang('core.login.username.required'),
						'length' => $this->base->lang('core.login.username.length'),
					),
				),
				'password' => array(
					'title' => $this->base->lang('core.login.password'),
					'required' => 1,
					'minlength' => 1,
					'maxlength' => 50,
					'messages' => array(
						'required' => $this->base->lang('core.login.password.required'),
						'length' => $this->base->lang('core.login.password.length'),
						'maxlength' => $this->base->lang('core.login.password.maxlength'),
					),
				),
			);
			if($this->base->lib->validation($rules) && $this->authenticate())
			{
				$id = $this->user->get_id_by_username($this->base->lib->input_post('username'));
				$this->base->session->create_member_session($id);
				$this->base->output->redirect_wait($this->base->lang('core.login.redirect.message'));
			}
			else
			{
				$this->base->output->process('forms.login');
				$this->base->output->setTitle($this->base->lang('pagetitles.login'));
				$this->base->output->flush();
			}
		}
		else
		{
			$this->base->output->redirect();
		}
	}

	public function logout()
	{
		if($this->base->session->logged_in())
		{
			$this->base->session->destroy_session();
			$this->base->output->redirect_wait($this->base->lang('core.logout.redirect.message'));
		}
		else
		{
			$this->base->output->redirect();
		}
	}

	function register()
	{
		$this->base->loadHandler('user');
		$rules = array(
			'username' => array(
				'title' => $this->base->lang('core.login.username'),
				'required' => 1,
				'minlength' => 4,
				'maxlength' => 50,
				'messages' => array(
					'required' => $this->base->lang('core.register.username.required'),
					'minlength' => $this->base->lang('core.register.username.minlength'),
					'maxlength' => $this->base->lang('core.register.username.maxlength'),
				),
			),
			'nickname' => array(
				'title' => $this->base->lang('core.register.nickname'),
				'required' => 1,
				'minlength' => 4,
				'maxlength' => 50,
				'messages' => array(
					'required' => $this->base->lang('core.register.nickname.required'),
					'minlength' => $this->base->lang('core.register.nickname.minlength'),
					'maxlength' => $this->base->lang('core.register.nickname.maxlength'),
				),
			),
		);
		if($this->base->lib->validation($rules) && $this->account_create())
		{
			$this->base->output->redirect_wait('Your Account has been created');
		}
		else
		{
			$this->base->output->process('forms.register');
			$this->base->output->setTitle($this->base->lang('pagetitles.register'));
			$this->base->output->flush();
		}
	}
	private function authenticate()
	{
		$result = $this->user->authenticate($this->base->lib->input_post('username'), $this->base->lib->input_post('password'));
		if(count($_POST) > 0 && !$result) $this->base->lib->validation_errors[] = $this->base->lang('core.login.fail');
		return $result;
	}

	private function account_create()
	{
		$result = $this->user->create(
		$this->base->lib->input_post('username'), 
		$this->base->lib->input_post('password'), 
		$this->base->lib->input_post('email'), 
		$this->base->lib->input_post('nickname'),
		$this->base->lib->input_post('gender')
		);
		if(count($_POST) > 0 && in_array($result, array('E_TAKEN_NICKNAME', 'E_TAKEN_EMAIL', 'E_TAKEN_USERNAME'))) $this->base->lib->validation_errors[] = $this->base->lang('core.register.fail');
		return $result;
	}
}

?>